<?php
class EverydayPriceAction extends CommonAction {
    public function _initialize() {
        header("Content-Type:text/html; charset=utf-8");
        $this->_inject_check(1);//调用过滤函数
        $this->_Config_name();//调用参数
    }

    public function managae_price($beginThismonth = 0) {
        //$beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
        //测试可以， 小于当前时间
        //$beginThismonth = strtotime('-10 minute'); //测试用，半个小时前

        ignore_user_abort(true);
        set_time_limit(600);

        $where              = [];
        $where['save']      = 0;
        $where['create']    = ['lt',$beginThismonth];
        $where['pdt']       = 0;
        $m_manage           = M('manage_price_temp');
        $all_manage_price   = $m_manage->where($where)->select();


        $fck = D('Fck');

        foreach ($all_manage_price as $rs) {
            $fck->rw_bonus($rs['uid'], $rs['in_uid'], $rs['action_type'], $rs['epoints'], $rs['bz_text']);

            $m_manage->where(['id' => $rs['id']])->save(['pdt' => time(), 'save' => 1]);
        }

        unset($m_manage);
        unset($all_manage_price);
        unset($fck);
        return true;
    }

    //管理奖 月结
    public function month_manage_price() {
        if($_POST) {
            $result = $this->checkToMonthPrice();
            $msg = $result ? '结算完成' : '没有结算';
            $this->assign('msg', $msg);
        }

        $this->display();
    }

    private function checkToMonthPrice() {
        $cachename      = 'manage_time';
        $manage_time    = S($cachename);//设置缓存标示
        $cache_time     = 60*60*24*5;//设置缓存标示
        $cache_time     = 30;   //设置缓存标示, 测试用

        if(!$manage_time) {
            $where          = [];
            $where['save']  = 0;
            $where['pdt']   = ['elt', 0];
            $m_manage       = M('manage_price_temp')->where($where)->order('`create`')->find();
            $manage_time = $m_manage ? $m_manage['create'] : 0;
            S($cachename, $manage_time, $cache_time); //设置缓存的生存时间
        }


        $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));
        //$beginThismonth = strtotime('-5 minute'); //测试用，半个小时前. 调整测试周期时间
        if($beginThismonth >$manage_time &&  $manage_time > 0) {
            if(S('LOKE_MANAGE')) {
                return false;   //只可以有一个用户引发运行一次
            } else {
                //S('LOKE_MANAGE', TRUE, 60*60); //设置缓存的生存时间
                S('LOKE_MANAGE', TRUE, $cache_time); //设置缓存的生存时间
            }

            A('EverydayPrice')->managae_price($beginThismonth);

            S($cachename,NULL); //删除缓存
            S('LOKE_MANAGE',NULL); //删除缓存

            return true;
        }

        return false;
    }
}
?>